package cn.xuchunh.campusbbs.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.view.ViewGroup;

import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.base.BaseRecyclerViewAdapter;
import cn.xuchunh.campusbbs.bean.Topic;
import cn.xuchunh.campusbbs.bean.ReplyContent;
import cn.xuchunh.campusbbs.utils.DateUtil;
import cn.xuchunh.campusbbs.viewholder.ReplyContentViewHolder;
import cn.xuchunh.campusbbs.viewholder.TopicHeaderViewHolder;

/**
 * Created by XuChunH on 2015/9/14.
 */
public class TopicDetailAdapter extends BaseRecyclerViewAdapter<ViewHolder, ReplyContent> {

    public static final int TYPE_HEADER = 0;
    public static final int TYPE_CONTENT = 1;
    private Topic topic;

    public TopicDetailAdapter(Context context, Topic topic) {
        super(context);
        this.topic = topic;
        dataList.addDataList(topic.getContent());
    }

    @Override
    public ViewHolder onCreateContentViewHolder(ViewGroup parent, int viewType) {
        ViewHolder viewHolder;
        if (viewType == TYPE_HEADER) {
            viewHolder = new TopicHeaderViewHolder(inflater.inflate(R.layout.item_topic_detail_header, parent, false));
        } else {
            viewHolder = new ReplyContentViewHolder(inflater.inflate(R.layout.item_topic_detail_content, parent, false));
        }
        return viewHolder;
    }

    @Override
    protected void onBindContentViewHolder(ViewHolder holder, int position) {
        if (holder.getItemViewType() == TYPE_HEADER) {
            configHeader(holder);
        }else {
            configContent(holder,position);
        }
    }

    private void configHeader(ViewHolder viewHolder){
        TopicHeaderViewHolder temp = (TopicHeaderViewHolder) viewHolder;
        temp.title.setText(topic.getTitle());
        temp.replies.setText(topic.getReplies() + "");
        temp.hits.setText(topic.getHits() + "");
        temp.date.setText(DateUtil.formatDate(topic.getCreateDate()));
        temp.username.setText(topic.getUserNickName());
        temp.userTitle.setText(topic.getUserTitle());
        temp.position.setText(R.string.topic_author);
        temp.avatar.setImageURI(Uri.parse(topic.getUserAvatar()));
        viewHolder = temp;
    }

    private void configContent(ViewHolder viewHolder, int position){
        ReplyContentViewHolder temp = (ReplyContentViewHolder)viewHolder;
        ReplyContent content = dataList.get(position - 1);
        switch (content.getType()){
            case ReplyContent.TYPE_EXTERNAL_LINK:
            case ReplyContent.TYPE_TEXT:
                temp.text.setText(content.getInfo());
                temp.text.setVisibility(View.VISIBLE);
                temp.image.setVisibility(View.GONE);
                break;
            case ReplyContent.TYPE_IMAGE:
                temp.text.setVisibility(View.GONE);
                temp.image.setVisibility(View.VISIBLE);
                temp.image.setImageURI(Uri.parse(content.getOriginalInfo()));
                break;
        }
        viewHolder = temp;

    }

    public void setTopic(Topic topic) {
        this.topic = topic;
        dataList.clear();
        dataList.addDataList(topic.getContent());
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public int getItemCount() {
        return dataList.size() + 1;
    }

    @Override
    public int getContentItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        } else {
            return TYPE_CONTENT;
        }
    }

}
